<template>
  <div id="globalHeader">
    <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" :ellipsis="false"
      @select="handleSelect">
      <el-menu-item index="0">
        <img style="width: 50px" src="../assets/xiaomi.svg" alt="Xiaomi logo" />
        <div class="title">售后维修服务工单履约</div>
      </el-menu-item>
      <div class="flex-grow" />
      <el-menu-item index="1" @click="handleIndex">首页</el-menu-item>
      <el-menu-item index="2">
        <el-tag>
          {{ user.state.user.role === "engineer" ? "工程师" : "客户" }}
        </el-tag>
        {{ user.state.user.name }}
      </el-menu-item>
      <el-sub-menu index="2">
        <template #title>
          <div class="block">
            <el-avatar :size="50" :src="avatar" />
          </div>
        </template>
        <el-menu-item index="2-1">个人中心</el-menu-item>
        <el-menu-item index="2-2" @click="handleLogout">退出登录</el-menu-item>
        <el-menu-item index="2-3">开发中</el-menu-item>
      </el-sub-menu>
    </el-menu>
  </div>
</template>

<script lang="ts" setup>
import { useUserStore } from "../store/user.js"
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router';
import avatar from "../assets/avatar.jpg"

const user = useUserStore();
const router = useRouter();

const activeIndex = ref('1')
const handleSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

const handleIndex = () => {
  router.push('/')
}

const handleLogout = () => {
  user.actions.logout();
  router.push('/user/login')
}
</script>

<style>
.flex-grow {
  flex-grow: 1;
}

.title {
  color: #444;
  margin-left: 16px;
}
</style>